﻿using CommunicationProtocol.dto.command.client;
using server.filter;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunicationProtocol.dto;
using System.Diagnostics;
using System.Drawing;


namespace monoClient.renderer.utils
{
    public class OtherPlayerRendererHelper
    {
        private ConcurrentDictionary<CommandExecutor, object> commandExecutors;



        public OtherPlayerRendererHelper(ConcurrentDictionary<CommandExecutor, object> commandExecutors)
        {
            this.commandExecutors = commandExecutors;
        }

        internal void execute(OtherPlayerPlantingCommand otherPlayerPlantingCommand)
        {
            OtherPlayerPlantingRenderer renderer = null;

            switch (otherPlayerPlantingCommand.status) {

                case 1: {
                        /*创建玩家操作渲染器*/
                        bool found = false;
                        foreach (var commd in commandExecutors.Keys)
                        {
                            if (commd is BuyablePlantRenderer)
                            {

                                BuyablePlantRenderer buyAblePlantRenderer = (BuyablePlantRenderer)commd;

                                StaticPlant targetPlant = buyAblePlantRenderer.StaticPlant;
                               

                                if ( targetPlant.ObjectId!= otherPlayerPlantingCommand.commandObjectId)
                                {
                                    return;
                                }

                                found = true;
                                renderer = new OtherPlayerPlantingRenderer(otherPlayerPlantingCommand, targetPlant);

                                commandExecutors.TryAdd(renderer,null);

                                break;
                            }

                        }



                        if (!found)
                        {
                            Debug.WriteLine($"未找到id = {otherPlayerPlantingCommand.commandObjectId}的植物");

                        }

                        break;
                    }
                case 2: {
                        find(otherPlayerPlantingCommand)
                            ?.updateLocation(otherPlayerPlantingCommand.xPixel, otherPlayerPlantingCommand.yPixel);

                        break;
                    }
                case 3:
                    {
                        renderer = find(otherPlayerPlantingCommand);
                        if (renderer != null) {

                            if(commandExecutors.TryRemove(renderer, out object val)){

                                Debug.WriteLine($"{otherPlayerPlantingCommand.operatorId} 已退出操作");
                            }
                        }
                        break;
                    }
                default: {
                        throw new NotSupportedException("不支持状态 "+ otherPlayerPlantingCommand.status); 
                    }
            }
        }

        internal OtherPlayerPlantingRenderer find(OtherPlayerPlantingCommand otherPlayerPlantingCommand) {

            foreach (var item in commandExecutors.Keys)
            {
                if (item is OtherPlayerPlantingRenderer)
                {

                    OtherPlayerPlantingRenderer otherPlayerRenderer = (OtherPlayerPlantingRenderer)item;

                    if (otherPlayerRenderer.otherPlayerPlantingCommand.commandObjectId == otherPlayerPlantingCommand.commandObjectId
                        && Equals(otherPlayerRenderer.otherPlayerPlantingCommand.operatorId, otherPlayerPlantingCommand.operatorId))
                    {
                        return otherPlayerRenderer;

                        
                    }
                }
            }

            return null;

        }
    }
}
